import axios from 'axios'
import {Message} from 'element-ui'
import {getToken} from '@/request/token'

const service = axios.create({
  // baseURL: "http://localhost:8081/",
  baseURL: "",
  timeout: 10000,
})

//request拦截器
service.interceptors.request.use(config => {
  if (getToken()) {
    config.headers['token'] = getToken()
  }
  return config
}, error => {

  Promise.reject(error)
})

// respone拦截器
service.interceptors.response.use(
  response => {

    const res = response.data;
    
    console.log(response.data);
    if(res.code == 200 || res.code == undefined)
      return response
    else{

      console.info("用户未登录")

      Message({
        type: 'warning',
        showClose: true,
        message: '未登录或登录超时，请重新登录哦'
      })

      return Promise.reject(res.msg);
    }
  },
  error => {
    Message({
      type: 'warning',
      showClose: true,
      message: '连接超时'
    })
    return Promise.reject('error')
  }
)

export default service
